package com.github.wxiaoqi.security.biz.rest.cms;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.biz.community.CommunitySocietyBiz;
import com.github.wxiaoqi.security.biz.entity.community.CommunitySociety;
import com.github.wxiaoqi.security.common.rest.BaseController;

import lombok.extern.slf4j.Slf4j;

/**
 * 小程序后台社群相关接口
 * @author Administrator
 *
 */
@Slf4j
@Controller
@RequestMapping("societies")
public class SocietyRestController extends BaseController<CommunitySocietyBiz, CommunitySociety> {

	@Autowired
	private CommunitySocietyBiz biz;
	
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Map<String, Object> query(@RequestBody Map<String, Object> param) {
		log.info("查询所有社群------start------{}", param);
		Map<String, Object> respMap = biz.querySocietyInfos(param);
		if (respMap.get("code").equals("00")) {
			respMap.put("success", true);
		}else {
			respMap.put("success", false);
		}
		log.info("查询所有社群------end------{}", respMap);
		return respMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(@RequestBody Map<String, Object> param) {
		log.info("添加社群------start------{}", param);
		Map<String, Object> respMap = new HashMap<>();
		try {
			String id = String.valueOf(SnowFlake.getId());
			String societyName = (String) param.get("societyName");
			String qrCode = (String) param.get("societyQr");
			String content = (String) param.get("societyContent");
			String merchantId = (String) param.get("merchantId");
			String platformId = (String) param.get("platformId");
			CommunitySociety society = new CommunitySociety();
			society.setId(id);
			society.setSocietyName(societyName);
			society.setSocietyQr(qrCode);
			society.setSocietyContent(content);
			society.setMerchantId(merchantId);
			society.setPlatformId(platformId);
			society.setCreateTime(new Timestamp(new Date().getTime()));
			biz.insert(society);
			respMap.put("code", "00");
			respMap.put("msg", "添加成功");
		} catch (Exception e) {
			respMap.put("code", "01");
			respMap.put("msg", "添加异常");
		}
		log.info("添加社群-----end-------------{}");
		return respMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Map<String, Object> delete(@RequestBody Map<String, Object> param) {
		log.info("删除社群------start------{}", param);
		Map<String, Object> respMap = biz.deleteSociety(param);
		if (respMap.get("code").equals("00")) {
			respMap.put("success", true);
		}else {
			respMap.put("success", false);
		}
		log.info("删除社群------end------{}", respMap);
		return respMap;
	}
}
